import axios from 'axios'
import { Message } from 'element-ui'
// 创建 axios 实例
const service = axios.create({
  // baseURL: process.env.BASE_URL, // url = base url + request url
  baseURL: process.env.NODE_ENV === 'production' ? process.env.VUE_APP_URL_PRODUCTION : process.env.VUE_APP_URL_DEVELOPMENT, // url = base url + request url
  timeout: 5000, // request timeout 5秒超时
})

//  请求拦截器
service.interceptors.request.use(
  (config) => {
    config.headers['Content-Type'] = 'application/json;charset=UTF-8'
    config.headers['Accept'] = 'application/json'
    config.headers['client_id'] = 'webApp' // 应用id
    config.headers['client_secret'] = 'webApp' // 应用密钥

    // config.headers['Request-From'] = 'mobile'
    let token = sessionStorage.getItem('loginToken') || null
    if (token) {
      // 如果token不为null，否则传token给后台 
      // config.headers['Authorization'] = token
      config.headers['access_token'] = token
    }
    return config
  },
  (error) => {
    return Promise.reject(error)
  }
)

// 响应拦截器
service.interceptors.response.use(
  (response) => {
    const res = response
    //届时根据后端返回success或者code值判断
    if (res.success === true) {
      return res
    } else {
      return res
    }
  },
  // 处理处理
  (error) => {
    let errInfo = "";
    if (error && error.response) {
      switch (error.response.status) {
        case 400:
          errInfo = '错误请求';
          break;
        case 401:
          errInfo = '未授权，请重新登录';
          break;
        case 403:
          errInfo = '拒绝访问';
          break;
        case 404:
          errInfo = '请求错误,未找到该资源';
          break;
        case 405:
          errInfo = '请求方法未允许';
          break;
        case 408:
          errInfo = '请求超时';
          break;
        case 500:
          errInfo = '服务器端出错';
          break;
        case 501:
          errInfo = '网络未实现';
          break;
        case 502:
          errInfo = '网络错误';
          break;
        case 503:
          errInfo = '服务不可用';
          break;
        case 504:
          errInfo = '网络超时';
          break;
        case 505:
          errInfo = 'http版本不支持该请求';
          break;
        default:
          errInfo = `未知错误${error.response.status}`;
      }
    } else {
      errInfo = "连接到服务器失败";
    }
    return Message({
      message: errInfo || 'error',
      type: 'error',
    });
  }
);
/*网络请求部分*/

/*
 *  get请求
 *  url:请求地址
 *  params:参数
 * */
export function get(url, params = {}) {
  return new Promise((resolve, reject) => {
    service({
          url: url,
          method: 'get',
          params: params
      }).then(response => {
          resolve(response);
      }).catch(error => {
          reject(error);
      });
  });
}

/*
*  post请求
*  url:请求地址
*  params:参数
* */
export function post(url, params = {}) {
  return new Promise((resolve, reject) => {
    service({
          url: url,
          method: 'post',
          data: params
      }).then(response => {
          resolve(response);
      }).catch(error => {
          reject(error);
      });
  });
}

/*
*  文件上传
*  url:请求地址
*  params:参数
* */
export function fileUpload(url, params = {}) {
  return new Promise((resolve, reject) => {
    service({
          url: url,
          method: 'post',
          data: params,
          headers: { 'Content-Type': 'multipart/form-data' }
      }).then(response => {
          resolve(response);
      }).catch(error => {
          reject(error);
      });
  });
}

export default {
  get,
  post,
  fileUpload
}